package com.zx.viewlearn.views;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ComposeShader;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.RadialGradient;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.zx.viewlearn.R;

/**
 * Created by zhoux on 2018/1/8.
 * 注释：自定义view onMeasure onDraw学习
 */

public class MyView9 extends View {
    Paint mPaint;
    Paint mArcPaint;
    final static int minWidth = 600;

    float cx1 = -1;
    float cy1 = -1;
    float radius = -1;

    public MyView9(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(getResources().getColor(R.color.red));
        mArcPaint = new Paint();
        mArcPaint.setStyle(Paint.Style.STROKE);
        mArcPaint.setStrokeWidth(80);
        mArcPaint.setColor(getResources().getColor(R.color.blue));

        cx1 = minWidth / 2;
        cy1 = minWidth / 2;
        radius = minWidth / 4;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        setMeasuredDimension(getMySize(widthMeasureSpec), getMySize(heightMeasureSpec));
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        int mRectHeight = getHeight();
        SweepGradient mLinearGradient = new SweepGradient(cx1, cy1, new int[]{Color.RED, Color.YELLOW, Color.BLUE}, null);//扫描渐变
        RadialGradient mRadialGradient = new RadialGradient(cx1, cy1, radius, new int[]{Color.RED, Color.YELLOW, Color.BLUE}, null, Shader.TileMode.CLAMP);
//        RadialGradient //径向渐变 中心向两边模糊
//        ComposeShader 组合
        LinearGradient mLinearGradient2 = new LinearGradient(0, 0, 0, mRectHeight, new int[]{Color.RED, Color.YELLOW, Color.BLUE}, null, Shader.TileMode.CLAMP);//线性渐变
        Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_test);
        BitmapShader mBitmapShader = new BitmapShader(mBitmap, Shader.TileMode.REPEAT,
                Shader.TileMode.MIRROR);
        ComposeShader mComposeShader = new ComposeShader(mBitmapShader, mRadialGradient, PorterDuff.Mode.DARKEN);//组合渐变
        mPaint.setShader(mComposeShader);
        mArcPaint.setShader(mLinearGradient2);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        if (cy1 == -1) {
            drawView(canvas);
//            move1();
        } else {
            drawView(canvas);

        }
    }

    float mSweepAngle = 290;

    public void setmSweepAngle(float mSweepAngle) {
        if (mSweepAngle != 0) {
            this.mSweepAngle = mSweepAngle;

        } else {
            this.mSweepAngle = 25;

        }
        this.invalidate();
    }

    private void drawView(Canvas canvas) {
        mPaint.setColor(getResources().getColor(R.color.colorAccent));
        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(cx1, cy1, 100, mPaint);
        RectF mRectF = new RectF((float) (minWidth * 0.1), (float) (minWidth * 0.1), (float) (minWidth * 0.9), (float) (minWidth * 0.9));//外切矩形，用来控制圆环形状
        canvas.drawArc(mRectF/*外切矩形，用来控制圆环形状*/,
                270/*开始绘制的角度*/,
                mSweepAngle/*结束绘制的角度*/,
                false/*是否连接圆心*/,
                mArcPaint/*画笔*/);
    }


    public static int getMySize(int measureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        Log.e("MyView3", "specMode:" + specMode);
        Log.e("MyView3", "specSize:" + specSize);//当没有给固定值时候(match_parent，wrap_content)，specSize给的是充满父控件的值。
        switch (specMode) {
            case MeasureSpec.EXACTLY://view设置成match_parent时候，会是这个模式.
                Log.e("MyView3", "MeasureSpec.EXACTLY:" + MeasureSpec.EXACTLY);//1073741824
                result = specSize;
                break;
            case MeasureSpec.AT_MOST://view设置成wrap_content时候，会是这个模式
                Log.e("MyView3", "MeasureSpec.AT_MOST:" + MeasureSpec.AT_MOST);//-2147483648
                result = minWidth;
                result = Math.min(result, specSize);//设置成wrap_content时候，给一个默认值，这样就不会充满屏幕了
                break;
            case MeasureSpec.UNSPECIFIED:
                Log.e("MyView3", "MeasureSpec.UNSPECIFIED:" + MeasureSpec.UNSPECIFIED);//0
                result = specSize;
                break;
            default:
                break;
        }
        return result;

    }


}
